package com.train.mgr.modules.biz.controller;

import com.train.common.model.R;
import com.train.mgr.modules.biz.dto.response.AiAnalyzeResp;
import com.train.mgr.modules.biz.dto.response.AudioEvalDto;
import com.train.mgr.modules.biz.dto.response.AudioTextRespVo;
import com.train.mgr.modules.biz.dto.response.HeadVisualResp;
import com.train.mgr.modules.biz.entity.LessonUserExercise;
import com.train.mgr.modules.biz.service.IAliQwenService;
import com.train.mgr.modules.biz.service.IAnalysisCalService;
import com.train.mgr.modules.biz.service.IxfyunLfasrClientService;
import com.train.mgr.modules.biz.thirdparty.client.FaceAnalyzeClient;
import com.train.mgr.modules.biz.thirdparty.client.VoiceAnalyzeClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 以下为测试代码，后期可删除
 */
@Api(value = "TestController", tags = "调试接口入口")
@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private VoiceAnalyzeClient voiceAnalyzeClient;

    @Autowired
    private FaceAnalyzeClient faceAnalyzeClient;

    @Autowired
    private IxfyunLfasrClientService IxfyunLfasrClientService;

    @Autowired
    private IAliQwenService aliQwenService;

    @Autowired
    private IAnalysisCalService analysisCalService;

    @ApiOperation("5维度分数计算")
    @GetMapping(value = "/analysis_cal")
    public R<AudioEvalDto> analysisCal(@RequestParam(name = "lessonId", required = true) Long lessonId,
                                       @RequestParam(name = "exerciseId", required = true) Long exerciseId,
                                       @RequestParam(name = "useTime", required = true) String useTime,
                                       @RequestParam(name = "audioFilePath", required = true) String audioFilePath){
        LessonUserExercise bean = new LessonUserExercise();
        bean.setId(exerciseId);
        bean.setLessonId(lessonId);
        bean.setUseTime(useTime);
        bean.setExerciseType(2);
        AudioEvalDto resp = analysisCalService.getCalScoreMap(bean,  audioFilePath);
        return R.succeed(resp);
    }

    @ApiOperation("语音流畅性评估分析API")
    @PostMapping(value = "/voice_analyze")
    public R<Object> voiceAnalyze(@RequestParam(name = "audioFilePath", required = true) String audioFilePath,
                                  @RequestParam(name = "exerciseId", required = true) Long exerciseId,
                                  @RequestParam(name = "transcriptRef", required = true) String transcriptRef){
        String url = "/data/uploadfiles"+audioFilePath;
        File audioFile = new File(url);
        AudioTextRespVo textResp = IxfyunLfasrClientService.getAudioText(exerciseId,url);
        String transcriptHyp = textResp.getAudioText();
        log.info("*********************** transcriptRef:{}",transcriptRef);
        log.info("*********************** transcriptHyp:{}",transcriptHyp);
        AiAnalyzeResp resp = voiceAnalyzeClient.readAnalyze(audioFile,transcriptRef,transcriptHyp,"",2.0f, -40.0f);
        return R.succeed(resp);
    }

    @ApiOperation("视频人脸特征分析API")
    @PostMapping(value = "/face_analyze")
    public R<Object> faceAnalyze(){
        List<File> imagesFileList = Arrays.asList(
                new File("/Users/tianyi/Documents/logo/1.png"),
                new File("/Users/tianyi/Documents/logo/2.png")
        );
        HeadVisualResp resp = faceAnalyzeClient.faceAnalyze(imagesFileList);
        return R.succeed(resp);
    }

    @ApiOperation("讯飞接口-测试视频转文本")
    @PostMapping(value = "/voice_to_text")
    public R<AudioTextRespVo> testAudioToText( @RequestParam(name = "exerciseId", required = true) Long exerciseId) {
        AudioTextRespVo bean = IxfyunLfasrClientService.getAudioText(exerciseId,"/Users/tianyi/Documents/logo/1111111.wav");
        log.info("---------2-----",bean.getAudioText());
        return R.succeed(bean,"完成");
    }

    @ApiOperation("阿里通义千问-文本生成")
    @GetMapping(value = "/call_qwen")
    public R<String> getQwenFeekBack(@RequestParam(name = "prompt",required = true) String prompt) {
        return R.succeed(aliQwenService.getQwenFeedBack(prompt));
    }

}
